package com.hainiu.cat.interview.juc;

import java.util.UUID;
import java.util.concurrent.CopyOnWriteArraySet;
import java.util.concurrent.TimeUnit;

/**
 * create by biji.zhao on 2021/7/11
 */
public class CollectTest {

    public static void main(String[] args) {
        CopyOnWriteArraySet<String> arraySet = new CopyOnWriteArraySet<>();

        for (int i = 1; i <= 10; i++) {
            final int temp = i;
            new Thread(() -> {
                String substring = UUID.randomUUID().toString().substring(0, 10);
                String name = Thread.currentThread().getName();
                System.out.println(substring + "----> 线程" + name);
                arraySet.add(substring + "----> 线程" + name);
                System.out.println("线程：" + name + "数组长度：" + arraySet.size() + temp);
            }, String.valueOf(i)).start();
        }

        try {
            TimeUnit.SECONDS.sleep(3);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }

        System.out.println("===================================");
        arraySet.forEach(e -> {
            System.out.println(e);
        });
    }
}
